home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14399 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  98 lines

  1. Path: jaxnet.jaxnet.com!NewsWatcher!user
  2. From: garyg@jax.jaxnet.com (Gary M. Greenberg)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Can anyone help a newbie out ?
  5. Date: Sun, 14 Apr 1996 11:18:00 -0400
  6. Organization: Southeast Network Services, Inc.
  7. Message-ID: <garyg-1404961118000001@204.183.221.242>
  8. References: <4kkf3r$5b0@darwin.nbnet.nb.ca>
  9. NNTP-Posting-Host: ts4-000.jaxnet.com
  10.  
  11. In article <4kkf3r$5b0@darwin.nbnet.nb.ca>, lewwid@brunswickmicro.nb.ca
  12. (Jeff) wrote:
  13. >  Problem:Write a function that accepts two strings.  Count the number
  14. >  of characters in each string, and return the pointer to the longer
  15. >  string.
  16. [code snipped]
  17.  
  18.     [courtesy copy emailed]
  19.  
  20. Here's my very basic illustration to demonstrate a possible solution.
  21.  
  22. It doesn't return a pointer to the longer string because
  23. the problem doesn't account for equal length inputs.
  24.  
  25. Instead, it returns a string which states the size relationship.
  26. However, I think the code is in keeping with the spirit of the Problem.
  27. /*** Flamers, start your engines ;-) ***/
  28.  
  29. /* longerstr.c */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #define SAME "the same length as"
  35. #define SH "shorter than"
  36. #define LO "longer than"
  37.  
  38. #define LEN 256 /* an _arbitrary_ string length limit */
  39.  
  40. /* prototype */
  41. char *compstrings (char *firststring, char *secstring);
  42.  
  43. int main()
  44. {
  45.     char str1[LEN], str2[LEN];
  46.     printf("Enter a string <STR1> ...\n");
  47.     /* one should AVOID gets */
  48.     fgets(str1,sizeof(str1),stdin);
  49.     if(str1[strlen(str1)-1]=='\n')
  50.         str1[strlen(str1)-1]='\0';
  51.     printf("Enter a string <STR2> ...\n");
  52.     fgets(str2,sizeof(str2),stdin);
  53.     if(str2[strlen(str2)-1]=='\n')
  54.         str2[strlen(str2)-1]='\0';
  55.     printf("String '%s' <STR1> is %s string '%s' <STR2>\n",str1,
  56.         compstrings(str1,str2),str2);
  57.     return EXIT_SUCCESS;
  58. }
  59.  
  60. char *compstrings(char *firststring, char *secstring)
  61. {
  62.     char *res;
  63.     
  64.     if(strlen(firststring)==strlen(secstring))
  65.         if((res=malloc(strlen(SAME)))==NULL) {
  66.             printf("No memory for SAME.\n");
  67.             exit EXIT_FAILURE;
  68.         }
  69.         else
  70.             strcpy(res,SAME);
  71.     
  72.     else if (strlen(firststring)>strlen(secstring))
  73.         if((res=malloc(strlen(LO)))==NULL) {
  74.             printf("No memory for LO.\n");
  75.             exit EXIT_FAILURE;
  76.         }
  77.         else
  78.             strcpy(res,LO);
  79.             
  80.     else
  81.     {
  82.             if((res=malloc(strlen(SH)))==NULL) {
  83.             printf("No memory for SH.\n");
  84.             exit EXIT_FAILURE;
  85.         }
  86.         else
  87.             strcpy(res,SH);
  88.     }
  89.     
  90.     return res;
  91. }
  92. /* end of longerstr.c */
  93.  
  94. gary    /* the Sorcerer's Apprentice */
  95.      Contribute to the Randal Schwatrz Legal Defense Fund by
  96.     Visit this site && grab free E-Mail Management Source Code
  97.            http:/jax.jaxnet.com/~garyg/main_page.html
  98.